package com.meetingshow.meeting.actions;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.meetingshow.common.PageBean;
import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.meeting.uiservice.IUIMeetingService;
import com.meetingshow.meeting.uiservice.model.MeetingQueryParasVO;
import com.meetingshow.meeting.uiservice.model.MeetingVO;
import com.meetingshow.privilege.uiservice.model.UserVO;
import com.meetingshow.utils.UIServiceFactory;

/**
 * 网站前端访问
 * 
 * @author ivan
 * 
 */
@ParentPackage("json-default")
@Namespace("/meeting")
public class MeetingWebAction extends MeetingBaseAction {
    /**
     * 
     */
    private static final long serialVersionUID = -6614278607561946981L;
    private final static Logger logger = Logger.getLogger(MeetingWebAction.class);
    
    private MeetingQueryParasVO meetingQueryParasVO = new MeetingQueryParasVO();
    private MeetingVO meetingVO;
    private String message;
    private PageBean<MeetingVO> pageBean = new PageBean<MeetingVO>();
    
    /**
     * 获取推荐会议
     * @return
     */
    @Action(value="rec",
            results={
                @Result(type="json", params={"root", "pageBean"})
            }
    )
    public String getRecmd() {
        try {
            initPageParam();
            QueryResults<MeetingVO> recmdMetQueryResults = getRecommendMeetings(meetingQueryParasVO);
            pageBean.setObjectList(recmdMetQueryResults.getResultList());
            pageBean.setTotalCount(String.valueOf(recmdMetQueryResults.getTotalResultsNum()));
            pageBean.setFirstresultno(meetingQueryParasVO.getFirstresultno());
            pageBean.setResultspergroup(meetingQueryParasVO.getResultspergroup());
        } catch (Exception e) {
            logger.error(e);
            message = "获取推荐会议失败";
            return SUCCESS;
        }
        
        return SUCCESS;
    }

    /**
     * 获取最近会议
     * @return
     */
    @Action(value="recent",
            results={
                @Result(type="json", params={"root", "pageBean"})
            }
    )
    public String getRecent() {
        initPageParam();
        meetingQueryParasVO.setOrderByCreateTime(MeetingQueryParasVO.DESC_ORDER);
        try {
            QueryResults<MeetingVO> recentMetQueryResults = getMeetings(meetingQueryParasVO);
            pageBean.setObjectList(recentMetQueryResults.getResultList());
            pageBean.setTotalCount(String.valueOf(recentMetQueryResults.getTotalResultsNum()));
            pageBean.setFirstresultno(meetingQueryParasVO.getFirstresultno());
            pageBean.setResultspergroup(meetingQueryParasVO.getResultspergroup());
        } catch (Exception e) {
            logger.error(e);
            message = "获取最近会议失败";
            return SUCCESS;
        }
        
        return SUCCESS;
    }
    
    /**
     * Get the detail of meeting.
     * @return
     */
    @Action(value="detail",
            results={
                @Result(type="json", params={"root", "meetingVO"})
            }
    )
    public String getMeetingDetail() {
        try {
            meetingVO = getMeetingById(Integer.valueOf(meetingQueryParasVO.getMeetingId_e()));
        } catch (Exception e) {
            logger.error(e);
            message = "获取会议详细失败";
        }
        return SUCCESS; 
    }
    
    /**
     * Initialize the pager parameter.
     */
    public void initPageParam() {
        if (StringUtils.isBlank(meetingQueryParasVO.getFirstresultno())) {
            meetingQueryParasVO.setFirstresultno(String.valueOf(PageBean.SINGLE_PAGE_CUR_PAGE - 1));
        }
        
        if (StringUtils.isBlank(meetingQueryParasVO.getResultspergroup())) {
            meetingQueryParasVO.setResultspergroup(String.valueOf(PageBean.SINGLE_PAGE_ROWS_MAX));
        }
    }
    
    /**
     * User follow the meeting.
     * @return
     */
    @Action(value="follow",
            results={
                @Result(type="json", params={"root", "message"})
            }
    )
    public String followMeeting() {
        UserVO userVO = getLoginUser();
        if(userVO == null) {
            message = "-2";
            return SUCCESS;
        }
        IUIMeetingService uiMeetingService = UIServiceFactory.getUIMeetingService();
        int count = 0;
        try {
            count = uiMeetingService.addFollowCount(meetingVO.getMeetingId(), userVO.getUserId());
        } catch (BaseUIServiceException e) {
            logger.error(e);
            message = "-1";
            return SUCCESS;
        }
        message = String.valueOf(count);
        return SUCCESS;
    }

    /**
     * @return the meetingQueryParasVO
     */
    public MeetingQueryParasVO getMeetingQueryParasVO() {
        return meetingQueryParasVO;
    }

    /**
     * @param meetingQueryParasVO the meetingQueryParasVO to set
     */
    public void setMeetingQueryParasVO(MeetingQueryParasVO meetingQueryParasVO) {
        this.meetingQueryParasVO = meetingQueryParasVO;
    }

    /**
     * @return the meetingVO
     */
    public MeetingVO getMeetingVO() {
        return meetingVO;
    }

    /**
     * @param meetingVO the meetingVO to set
     */
    public void setMeetingVO(MeetingVO meetingVO) {
        this.meetingVO = meetingVO;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the pageBean
     */
    public PageBean<MeetingVO> getPageBean() {
        return pageBean;
    }

    /**
     * @param pageBean the pageBean to set
     */
    public void setPageBean(PageBean<MeetingVO> pageBean) {
        this.pageBean = pageBean;
    }
}
